{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "263f914c-9d67-4316-8b3d-03c3b99ba9d8",
   "metadata": {},
   "source": [
    "# SEC filing\n",
    "\n",
    "\n",
    ">The SEC filing is a financial statement or other formal document submitted to the U.S. Securities and Exchange Commission (SEC). Public companies, certain insiders, and broker-dealers are required to make regular SEC filings. Investors and financial professionals rely on these filings for information about companies they are evaluating for investment purposes.\n",
    ">\n",
    ">SEC filings data powered by [Kay.ai](https://kay.ai) and [Cybersyn](https://www.cybersyn.com/) via [Snowflake Marketplace](https://app.snowflake.com/marketplace/providers/GZTSZAS2KCS/Cybersyn%2C%20Inc).\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fc507b8e-ea51-417c-93da-42bf998a1195",
   "metadata": {},
   "source": [
    "## Setup\n",
    "\n",
    "\n",
    "First, you will need to install the `kay` package. You will also need an API key: you can get one for free at [https://kay.ai](https://kay.ai/). Once you have an API key, you must set it as an environment variable `KAY_API_KEY`.\n",
    "\n",
    "In this example, we're going to use the `KayAiRetriever`. Take a look at the [kay notebook](/docs/integrations/retrievers/kay) for more detailed information for the parameters that it accepts.`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f7b8c99c-0341-4f3c-912f-a11e98f7de71",
   "metadata": {},
   "outputs": [
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      " ········\n",
      " ········\n"
     ]
    }
   ],
   "source": [
    "# Setup API keys for Kay and OpenAI\n",
    "from getpass import getpass\n",
    "\n",
    "KAY_API_KEY = getpass()\n",
    "OPENAI_API_KEY = getpass()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "04ee2d6b-c2ab-4e15-8a8b-afaf6ef8c0f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "\n",
    "os.environ[\"KAY_API_KEY\"] = KAY_API_KEY\n",
    "os.environ[\"OPENAI_API_KEY\"] = OPENAI_API_KEY"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "c923bea0-585a-4f62-8662-efc167e8d793",
   "metadata": {},
   "source": [
    "## Example"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "0c504bcd-f6e0-4028-a797-b31fb4b6d027",
   "metadata": {},
   "outputs": [],
   "source": [
    "from langchain.chains import ConversationalRetrievalChain\n",
    "from langchain.retrievers import KayAiRetriever\n",
    "from langchain_openai import ChatOpenAI\n",
    "\n",
    "model = ChatOpenAI(model_name=\"gpt-3.5-turbo\")\n",
    "retriever = KayAiRetriever.create(\n",
    "    dataset_id=\"company\", data_types=[\"10-K\", \"10-Q\"], num_contexts=6\n",
    ")\n",
    "qa = ConversationalRetrievalChain.from_llm(model, retriever=retriever)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "977f158b-38d3-4b5f-9379-7cdd09436327",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-> **Question**: What are patterns in Nvidia's spend over the past three quarters? \n",
      "\n",
      "**Answer**: Based on the provided information, here are the patterns in NVIDIA's spend over the past three quarters:\n",
      "\n",
      "1. Research and Development Expenses:\n",
      "   - Q3 2022: Increased by 34% compared to Q3 2021.\n",
      "   - Q1 2023: Increased by 40% compared to Q1 2022.\n",
      "   - Q2 2022: Increased by 25% compared to Q2 2021.\n",
      "   \n",
      "   Overall, research and development expenses have been consistently increasing over the past three quarters.\n",
      "\n",
      "2. Sales, General and Administrative Expenses:\n",
      "   - Q3 2022: Increased by 8% compared to Q3 2021.\n",
      "   - Q1 2023: Increased by 14% compared to Q1 2022.\n",
      "   - Q2 2022: Decreased by 16% compared to Q2 2021.\n",
      "   \n",
      "   The pattern for sales, general and administrative expenses is not as consistent, with some quarters showing an increase and others showing a decrease.\n",
      "\n",
      "3. Total Operating Expenses:\n",
      "   - Q3 2022: Increased by 25% compared to Q3 2021.\n",
      "   - Q1 2023: Increased by 113% compared to Q1 2022.\n",
      "   - Q2 2022: Increased by 9% compared to Q2 2021.\n",
      "   \n",
      "   Total operating expenses have generally been increasing over the past three quarters, with a significant increase in Q1 2023.\n",
      "\n",
      "Overall, the pattern indicates a consistent increase in research and development expenses and total operating expenses, while sales, general and administrative expenses show some fluctuations. \n",
      "\n"
     ]
    }
   ],
   "source": [
    "questions = [\n",
    "    \"What are patterns in Nvidia's spend over the past three quarters?\",\n",
    "    # \"What are some recent challenges faced by the renewable energy sector?\",\n",
    "]\n",
    "chat_history = []\n",
    "\n",
    "for question in questions:\n",
    "    result = qa({\"question\": question, \"chat_history\": chat_history})\n",
    "    chat_history.append((question, result[\"answer\"]))\n",
    "    print(f\"-> **Question**: {question} \\n\")\n",
    "    print(f\"**Answer**: {result['answer']} \\n\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
